<?php
    $item_menu[0] = 5;
    $item_menu[1] = 2   ;
    $title = 'Editar Productos';    
	
    if (!defined('URL')) { include('header.php'); }
    
	if (!isset($_SESSION["admin"])) {redirect("login.php");	exit(); }



    $submit = request('submit','');
	$id     		= request('id',0);
	$familia_id 	= request('familia_id',0);


    if (!empty($submit)) {

        include('productos_guardar.php');

    } else {
    	$cond 	= "select * from productos where id='$id'";
		
    	$rs 	= $db->SelectLimit($cond, 1);
    	$Producto	= $rs->FetchRow();

    }
    
        $cond 	= "select * from productos_grupos where producto_id = '$id' ";
		$rs 	= $db->Execute($cond);
		$Pertenece	= $rs->GetRows();

        $cond 	= "select * from grupos where activo=1 order by nombre ASC";
    	$rs 	= $db->Execute($cond);
    	$xGrupos	= $rs->GetRows();
        $select_grupos = "<select name='productos_grupos[]' multiple style=\"width:150px; height:100px;\">";
        foreach ($xGrupos as $xgrupo) {
            if (!empty($Pertenece) ) {
                $existe = 0;
                foreach($Pertenece as $p){
                    if ($p['grupo_id'] == $xgrupo['id']) { $existe=1; break;}
                }
                if ($existe == 1) {
                    $select_grupos.= "<option value='{$xgrupo['id']}' selected='selected'>{$xgrupo['nombre']}</option>";
                } else {
                    $select_grupos.= "<option value='{$xgrupo['id']}'>{$xgrupo['nombre']}</option>";
                }
                
            } else {
                $select_grupos.= "<option value='{$xgrupo['id']}'>{$xgrupo['nombre']}</option>";    
            }
            
        }
        $select_grupos.= "</select>";
    

    // -ARMA LOS SELECT DE LAS FAMILIAS
    $Familias = crearArbol(0);
    $parent_id = 0;
    $familia1_id = '<select name="familia1_id" class="required">';
    foreach($Familias as $dd){
            if ($Producto['familia1_id'] == $dd['id']) { $sel = "selected='selected'"; } else {$sel='';}
            $separador = "";
            if ($dd['nivel'] == 2 ) {
                $separador = "------";
            } elseif ($dd['nivel'] == 3 ) {
                $separador = "------------";
            } elseif ($dd['nivel'] == 4 ) {
                $separador = "------------------";
            }
            $familia1_id.= "<option value='{$dd['id']}' $sel >$separador {$dd['nombre1']}</option>";
    }
    $familia1_id.= "</select>";

    $familia2_id = '<select name="familia2_id" >';
    $familia2_id.= "<option value='0' >Si lo desea, seleccione una familia</option>";
    foreach($Familias as $dd){
            if ($Producto['familia2_id'] == $dd['id']) { $sel = "selected='selected'"; } else {$sel='';}
            $separador = "";
            if ($dd['nivel'] == 2 ) {
                $separador = "------";
            } elseif ($dd['nivel'] == 3 ) {
                $separador = "------------";
            } elseif ($dd['nivel'] == 4 ) {
                $separador = "------------------";
            }
            $familia2_id.= "<option value='{$dd['id']}' $sel >$separador {$dd['nombre1']}</option>";
    }
    $familia2_id.= "</select>";

    $familia3_id = '<select name="familia3_id" >';
    $familia3_id.= "<option value='0' >Si lo desea, seleccione una familia</option>";    
    foreach($Familias as $dd){
            if ($Producto['familia3_id'] == $dd['id']) { $sel = "selected='selected'"; } else {$sel='';}
            $separador = "";
            if ($dd['nivel'] == 2 ) {
                $separador = "------";
            } elseif ($dd['nivel'] == 3 ) {
                $separador = "------------";
            } elseif ($dd['nivel'] == 4 ) {
                $separador = "------------------";
            }
            $familia3_id.= "<option value='{$dd['id']}' $sel >$separador {$dd['nombre1']}</option>";
    }
    $familia3_id.= "</select>";
    // --- FIN ARMADO SELECT DE FAMILIAS


?>
<script type="text/javascript" src="<?php echo URL;?>/admin/productos_editar.js"></script>
<script type="text/javascript" src="<?php echo URL;?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo URL;?>/js/jquery-validation-1.9.0/jquery.validate.js"></script>
<script type="text/javascript" src="<?php echo URL;?>/js/jquery-validation-1.9.0/localization/messages_es.js"></script>
<style type="text/css">
label { float: left; }
label.error { float: none; color: red; padding-left:0px; vertical-align: top; display: block;font-size:12px;}
p { clear: both; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; color:red;}

</style>

<script>
    $(document).ready(function(){
        $("#form1").validate({});

    });
</script>


<!-- Menu Pestañas -->
<script type="text/javascript" src="<?php echo URL;?>/js/menupestanas/menupestanas.js"></script>
<link rel="stylesheet" href="<?php echo URL;?>/js/menupestanas/menupestanas.css" type="text/css" media="screen" />    


<?php if(!empty($_SESSION['Msg'])) { echo mensaje_ok($_SESSION['Msg']); $_SESSION['Msg']=''; } ?>

<center>
<form name='form1' id='form1' action='productos_editar.php?id=<?php echo $id;?>' method='post' accept-charset="utf-8"> 

    <ul class="tabs">
        <li><a href="#p_datos">Datos Generales y SEO</a></li>                                               
        <li><a href="#p_contenido">Contenido</a></li>           
        <li><a href="#p_imagenes">Imagenes</a></li>           
    </ul>

    <div class="tab_container">
        <div id="p_datos" class="tab_content" style='min-height:400px;width:850px;background:#FFF;'>
            <?php include('productos_editar_datos.php'); ?>
        </div>
        <div id="p_contenido" class="tab_content" style='min-height:400px;width:850px;background:#FFF;'>
            <?php include('productos_editar_contenido.php'); ?>
        </div>
        <div id="p_imagenes" class="tab_content" style='min-height:400px;width:850px;background:#FFF;'>
            <?php include('productos_editar_imagenes.php'); ?>
        </div>
    </div>

    <input type='hidden' name='id' value='<?=$id;?>' />
    <input type='submit' name='submit' value='<< Guardar los cambios >>'  />
</form>
</center>
